package lv.accenture.ex04;

public class Hilbert {
	
    public String getPath(String initialStr) {
    	
    	String tClockw=new String();
    	String tCClockw=new String();
    	String resStr = new String();
    	String[] df=initialStr.split(",").clone();
    	tClockw="";
    	for (int i=0;i<df.length;i++){
    		if ((i<df.length)&&(i>0)) tClockw=tClockw+",";
    		
    		if (df[i].equals("v1")){
    			df[i]="h1";
    			tClockw=tClockw+df[i];
    			continue;
    		}
    		
    		if (df[i].equals("h1")){
    			df[i]="v1";
    			tClockw=tClockw+df[i];
    			continue;
    		}
    		
    		if (df[i].equals("v-1")){
    			df[i]="h-1";
    			tClockw=tClockw+df[i];
    			continue;
    		}
    		
    		if (df[i].equals("h-1")){
     			  df[i]="v-1";
    			  tClockw=tClockw+df[i];
    			  continue;
    		}	
     	}
    	//counterclockwise rotation
    	
    	tCClockw="";
    	df=initialStr.split(",").clone();
    	for (int i=0;i<df.length;i++){
    		if ((i<df.length)&&(i>0)) tCClockw=tCClockw+",";
    			if (df[i].equals("v1"))	{
    				df[i]="h-1";
    				tCClockw=tCClockw+df[i];
    				continue;
    			}
    		if (df[i].equals("h1")){
    			df[i]="v-1";
    			tCClockw=tCClockw+df[i];
    			continue;
    		}
    		
    		if (df[i].equals("v-1")){
       			df[i]="h1";
    			tCClockw=tCClockw+df[i];
    			continue;
    		}
    		
    		if (df[i].equals("h-1")){
     			 df[i]="v1";
     			 tCClockw=tCClockw+df[i];
    			  continue;
    			}
    	}    	

    	df=tCClockw.split(",").clone();
       	resStr=tClockw+",v1,"+initialStr+",h1,"+initialStr+",v-1,"+tCClockw;
    	
       	return resStr;
    }
    public String hilbertSVG(int order) {
		StringBuffer sBuf = new StringBuffer();
		String nPath;
		int h,w;
		
		sBuf.append("<?xml version='1.0'?>\n");
		sBuf.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\"\n");
		sBuf.append("\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n");
		
		//here comes view sizes

		h=(int) Math.pow(2,order);
		w=(int) Math.pow(2,order);
		sBuf.append("<svg width=\"10cm\" height='10cm' viewBox='0 0 "+w+" "+h+ "'>\n");
		sBuf.append("<title>Hilbert curve</title>\n");
		sBuf.append("<desc>This is a sample SVG file for Hilbert curve</desc>\n");
		
		//here comes rectangle sizes
		
		sBuf.append("<rect x='0' y='0' width='"+w+"' height='"+h+"' fill='#cccccc'/>\n");
		sBuf.append("<g transform=\"translate(0,"+w+") scale(1 -1)\"\n");
		sBuf.append("style=\"stroke:blue;stroke-width:0.01;fill:none;\">");
		
		//here we calculate nodes
		
		nPath="v1,h1,v-1";
		for (int i=1;i<order;i++){
			nPath=getPath(nPath);	
		}
				
		sBuf.append("\n<path d=\"M,0.5,0.5,"+nPath+"\"/>");
		sBuf.append("\n</g>");
		sBuf.append("\n</svg>");
				
		return sBuf.toString();
	}
}